package cn.easyutil.util.platform.KDBird;

import java.util.HashMap;
import java.util.Map;

import cn.easyutil.util.javaUtil.JsonUtil;
import cn.easyutil.util.javaUtil.StringUtil;
import cn.easyutil.util.platform.KDBird.bean.KDBirdLogisticsInfoBean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.easyutil.util.javaUtil.HttpUtil;

/**
 * 快递鸟快递
 * http://www.kdniao.com/
 * @author spc
 *
 */
public class KDBirdUtil {

	//请求url
    private static final String ReqURL="http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
	private String eBusinessID;
	private String appKey;
	
	public KDBirdUtil(String eBusinessID,String appKey){
		this.eBusinessID = eBusinessID;
		this.appKey = appKey;
	}
	
	public static KDBirdUtil build(String eBusinessID,String appKey){
		KDBirdUtil util = new KDBirdUtil(eBusinessID,appKey);
		return util;
	}

	/**
	 * 即时查询
	 * @param ShipperCode	物流公司编码
	 * @param LogisticCode	快递单号
	 */
	public KDBirdLogisticsInfoBean queryImmediate(String ShipperCode, String LogisticCode){
		Map<String, String> map = new HashMap<String, String>();
		map.put("ShipperCode", ShipperCode);
		map.put("LogisticCode", LogisticCode);
		//请求数据
		String data = JsonUtil.beanToJson(map);
		//请求参数
		Map<String, String> param = new HashMap<String, String>();
		param.put("RequestData", StringUtil.UrlEncode(data));
		param.put("EBusinessID", this.eBusinessID);
		param.put("RequestType", "1002");
		param.put("DataType", "2");
		param.put("DataSign", sign(map));
		HttpUtil util = new HttpUtil(ReqURL);
		util.setRequestParam(param);
		String result = util.doUrl();
		if(StringUtil.isEmpty(result)){
			return null;
		}
		KDBirdLogisticsInfoBean bean = JsonUtil.jsonToBean(result, KDBirdLogisticsInfoBean.class);
		JSONArray array = JSONObject.parseObject(result).getJSONArray("Traces");
		if(array==null || array.size()==0){
			return bean;
		}
		for (Object object : array) {
			Map<String, Object> info = JsonUtil.jsonToMap(JsonUtil.beanToJson(object));
			KDBirdLogisticsInfoBean.KDBirdAddressInfoBean address = new KDBirdLogisticsInfoBean().new KDBirdAddressInfoBean();
			if(info==null || info.isEmpty()){
				continue;
			}
			if(info.get("AcceptStation") != null){
				address.setAcceptStation(info.get("AcceptStation").toString());
			}
			if(info.get("AcceptTime") != null){
				address.setAcceptTime(info.get("AcceptTime").toString());
			}
			if(info.get("Remark") != null){
				address.setRemark(info.get("Remark").toString());
			}
			bean.getInfo().add(address);
		}
		return bean;
	}
	
	/**
	 * 快递鸟加签
	 * @param data	请求参数
	 * @return
	 */
	private String sign(@SuppressWarnings("rawtypes") Map data){
		return StringUtil.UrlEncode(StringUtil.base64Encode(StringUtil.toMD5(JsonUtil.beanToJson(data)+this.appKey)));
	}
	
	public static void main(String[] args) {
		String str = "http://qiniuvideo.mxingm.com/QQ截图20191113161326.png,http://qiniuvideo.mxingm.com/O1CN01j3TCMz2LTRcS6I4jr_!!1970919693.png";
		String[] split = str.split(",");
		String img = null;
		for (String s : split) {
			if(!StringUtil.isEmpty(s)){
				img = s;
				break;
			}
		}
		System.out.println(img);
	}
	
	public String geteBusinessID() {
		return eBusinessID;
	}

	public void seteBusinessID(String eBusinessID) {
		this.eBusinessID = eBusinessID;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}
	
}
